'use strict';

angular.module('app')
    .controller('editPasswordController', ['$scope', '$rootScope', '$state', '$timeout',
        function($scope, $rootScope, $state, $timeout) {
            $scope.title = $rootScope.title = "修改密码";
            $scope.loading = true;
            $scope.record = {};
            $scope.record.userID = $state.params.userId;
            $scope.saveData =   function (){
               $("#userform").submit();
            }


            $("#userform").validate({
                rules: {
                    oldpassword:{
                        required: true
                    },
                    Password:{
                        required: true,
                        maxlength: 16
                    },
                    confirmPassword:{
                        required: true,
                        maxlength: 16,
                        equalTo: "#password"
                    }
                },
                messages: {
                    oldpassword:{
                        required: '旧密码不能为空',
                    },
                    Password:{
                        required: '请填写新密码',
                        maxlength: '密码长度不可大于16位'
                    },
                    confirmPassword:{
                        required: '请填写确认密码',
                        maxlength: '密码长度不可大于16位',
                        equalTo: '两次输入的密码不相符'
                    }
                },
                submitHandler:function(){
                    var m = $scope.record;
                    if(m){
                        $scope.isDisabled = true;//提交disabled
                        $.ajax({
                            url : $scope.app.project_name+'/registerUser/editPassword',
                            data: m
                        }).then(callback);
                    }

                    function callback(result){
                        if(result.httpCode ==200){//成功
                            if(result.data.userN>0){
                                alert(result.data.mesg);
                                $timeout(function(){
                                    $state.go('main.user.registerUser.userList');
                                },2000);
                            }else {
                                alert(result.data.mesg);
                                $scope.isDisabled = false;
                            }
                        }else{
                            alert("请求失败");
                            $scope.isDisabled = false;
                        }
                        $scope.loading = false;
                    }
                }
            });


        }]);